/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddBoothProduct.java
 *
 * Created on Mar 22, 2011, 9:55:51 AM
 */

package GUI;

import Business.BoothProductManage;
import Entities.BoothProduct;
import java.sql.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author Nhit
 */
public class AddBoothProduct extends javax.swing.JDialog {

    /** Creates new form AddBoothProduct */
    public AddBoothProduct(java.awt.Frame parent, boolean modal, int id, String name) {
        super(parent, modal);
        initComponents();
        this.setLocationRelativeTo(null);
        fillCategory();
        fillBooth();
        fillSizeUnit();
        fillWeightUnit();
        idProduct = id;
        fillDataToEdit();
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, name, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N
        
    }
    private void fillDataToEdit(){
        if(idProduct != 0){
            BoothProductManage  objProductManage = new BoothProductManage();
            BoothProduct objProduct = objProductManage.getProductById(idProduct);
            String size = objProduct.getSize();
            String[] arrSize = size.split("x");
            txtName.setText(objProduct.getName());
            txtSizeX.setText(arrSize[0]);
            txtSizeY.setText(arrSize[1]);
            txtSizeZ.setText(arrSize[2]);
            txfCost.setText(objProduct.getCost()+"");
            txtWeight.setText(objProduct.getWeight()+"");
            txaDescription.setText(objProduct.getDescription());
            cbbSizeUnit.setSelectedItem(objProductManage.getSizeUnitById(objProduct.getSizeUnitMeasureId()));
            cbbBoothName.setSelectedItem(objProductManage.getBoothNameById(objProduct.getBoothID()));
            cbbWeight.setSelectedItem(objProductManage.getWeightUnitById(objProduct.getWeightUnitMeasureId()));
            cbbCategory.setSelectedItem(objProductManage.getCategoryNameById(objProduct.getCategoryID()));
            cbbStatus.setSelectedItem(objProduct.getStatus());
            
        }
    }
    private void fillCategory(){
        cbbCategory.removeAllItems();
        BoothProductManage objProductManage = new BoothProductManage();
        //objProductManage.getCategory();
        for(String category : objProductManage.getCategory()){
            cbbCategory.addItem(category);
        }
    }
    private void fillSizeUnit(){
        cbbSizeUnit.removeAllItems();
        BoothProductManage objProductManage = new BoothProductManage();
        //objProductManage.getCategory();
        for(String unit : objProductManage.getSizeUnit()){
            cbbSizeUnit.addItem(unit);
        }
    }
    private void fillWeightUnit(){
        cbbWeight.removeAllItems();
        BoothProductManage objProductManage = new BoothProductManage();
        //objProductManage.getCategory();
        for(String unit : objProductManage.getWeightUnit()){
            cbbWeight.addItem(unit);
        }
    }
    private void fillBooth(){
        cbbBoothName.removeAllItems();
        BoothProductManage objProductManage = new BoothProductManage();
        //objProductManage.getCategory();
        for(String name : objProductManage.getBooth()){
            cbbBoothName.addItem(name);
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        lblName = new javax.swing.JLabel();
        lblCost = new javax.swing.JLabel();
        lblSize = new javax.swing.JLabel();
        lblWeight = new javax.swing.JLabel();
        lblDescription = new javax.swing.JLabel();
        lblCategory = new javax.swing.JLabel();
        lblBoothname = new javax.swing.JLabel();
        lblStatus = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        txtName = new javax.swing.JTextField();
        lblMsName = new javax.swing.JLabel();
        txfCost = new javax.swing.JFormattedTextField();
        USD = new javax.swing.JLabel();
        lblMsCost = new javax.swing.JLabel();
        txtSizeX = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtSizeY = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtSizeZ = new javax.swing.JTextField();
        cbbSizeUnit = new javax.swing.JComboBox();
        lblMsSize = new javax.swing.JLabel();
        txtWeight = new javax.swing.JTextField();
        cbbWeight = new javax.swing.JComboBox();
        lblMsWeight = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaDescription = new javax.swing.JTextArea();
        cbbCategory = new javax.swing.JComboBox();
        lblMsCategory = new javax.swing.JLabel();
        cbbBoothName = new javax.swing.JComboBox();
        lblMsBoothName = new javax.swing.JLabel();
        cbbStatus = new javax.swing.JComboBox();
        btnSave = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Add Booth Product", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N

        lblName.setText("Name");

        lblCost.setText("Cost");

        lblSize.setText("Size");

        lblWeight.setText("Weight");

        lblDescription.setText("Description");

        lblCategory.setText("Category");

        lblBoothname.setText("Booth Name");

        lblStatus.setText("Status");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblName)
                    .addComponent(lblCost)
                    .addComponent(lblStatus)
                    .addComponent(lblBoothname)
                    .addComponent(lblCategory)
                    .addComponent(lblDescription)
                    .addComponent(lblWeight)
                    .addComponent(lblSize))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblName)
                .addGap(18, 18, 18)
                .addComponent(lblCost)
                .addGap(18, 18, 18)
                .addComponent(lblSize)
                .addGap(12, 12, 12)
                .addComponent(lblWeight)
                .addGap(18, 18, 18)
                .addComponent(lblDescription)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
                .addComponent(lblCategory)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblBoothname)
                .addGap(18, 18, 18)
                .addComponent(lblStatus)
                .addGap(21, 21, 21))
        );

        jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 5, 10));

        txtName.setPreferredSize(new java.awt.Dimension(200, 20));
        jPanel3.add(txtName);

        lblMsName.setPreferredSize(new java.awt.Dimension(100, 20));
        jPanel3.add(lblMsName);

        txfCost.setPreferredSize(new java.awt.Dimension(200, 20));
        jPanel3.add(txfCost);

        USD.setText("USD");
        jPanel3.add(USD);

        lblMsCost.setPreferredSize(new java.awt.Dimension(50, 20));
        jPanel3.add(lblMsCost);

        txtSizeX.setPreferredSize(new java.awt.Dimension(40, 20));
        jPanel3.add(txtSizeX);

        jLabel9.setText("x");
        jPanel3.add(jLabel9);

        txtSizeY.setPreferredSize(new java.awt.Dimension(40, 20));
        jPanel3.add(txtSizeY);

        jLabel12.setText("x");
        jPanel3.add(jLabel12);

        txtSizeZ.setPreferredSize(new java.awt.Dimension(40, 20));
        jPanel3.add(txtSizeZ);

        cbbSizeUnit.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel3.add(cbbSizeUnit);

        lblMsSize.setPreferredSize(new java.awt.Dimension(60, 14));
        jPanel3.add(lblMsSize);

        txtWeight.setPreferredSize(new java.awt.Dimension(100, 20));
        jPanel3.add(txtWeight);

        cbbWeight.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel3.add(cbbWeight);

        lblMsWeight.setPreferredSize(new java.awt.Dimension(100, 14));
        jPanel3.add(lblMsWeight);

        jScrollPane1.setPreferredSize(new java.awt.Dimension(300, 50));

        txaDescription.setColumns(20);
        txaDescription.setRows(5);
        txaDescription.setPreferredSize(new java.awt.Dimension(164, 50));
        jScrollPane1.setViewportView(txaDescription);

        jPanel3.add(jScrollPane1);

        cbbCategory.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbbCategory.setPreferredSize(new java.awt.Dimension(200, 20));
        jPanel3.add(cbbCategory);

        lblMsCategory.setPreferredSize(new java.awt.Dimension(100, 14));
        jPanel3.add(lblMsCategory);

        cbbBoothName.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbbBoothName.setPreferredSize(new java.awt.Dimension(200, 20));
        jPanel3.add(cbbBoothName);

        lblMsBoothName.setPreferredSize(new java.awt.Dimension(100, 14));
        jPanel3.add(lblMsBoothName);

        cbbStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Normal", "Block" }));
        cbbStatus.setPreferredSize(new java.awt.Dimension(100, 20));
        jPanel3.add(cbbStatus);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 313, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28))
        );

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnReset.setText("Reset");

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(102, 102, 102)
                        .addComponent(btnSave)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnReset)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnCancel))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 431, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSave)
                    .addComponent(btnReset)
                    .addComponent(btnCancel))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        Date todayD=new Date(System.currentTimeMillis()); 
        BoothProductManage objProductManage = new BoothProductManage();
        BoothProduct objProduct = new BoothProduct();
        objProduct.setBoothID(objProductManage.getBoothIdByName(cbbBoothName.getSelectedItem().toString()));
        objProduct.setCategoryID(objProductManage.getCategoryIdByName(cbbCategory.getSelectedItem().toString()));
        objProduct.setCost(Float.parseFloat(txfCost.getText()));
        objProduct.setDescription(txaDescription.getText());
        objProduct.setId(idProduct);
        objProduct.setModifiedDate(todayD);
        objProduct.setName(txtName.getText());
        objProduct.setSize(txtSizeX.getText()+"x"+txtSizeY.getText()+"x"+txtSizeZ.getText());
        objProduct.setSizeUnitMeasureId(objProductManage.getSizeIdByUnit(cbbSizeUnit.getSelectedItem().toString()));
        objProduct.setStatus(cbbStatus.getSelectedIndex());
        objProduct.setWeight(Integer.parseInt(txtWeight.getText()));
        objProduct.setWeightUnitMeasureId(objProductManage.getWeightIdByUnit(cbbWeight.getSelectedItem().toString()));
        if(idProduct == 0){
            if(objProductManage.add(objProduct)){
                JOptionPane.showMessageDialog(rootPane, "Add succ!");
                this.dispose();
            }else{
                JOptionPane.showMessageDialog(rootPane, "Add error!");
                
            }
        }else{
            if(objProductManage.update(objProduct)){
                JOptionPane.showMessageDialog(rootPane, "Update succ!");
                this.dispose();
            }else{
                JOptionPane.showMessageDialog(rootPane, "Update error!");
               
            }
        
        }
        
    }//GEN-LAST:event_btnSaveActionPerformed

    /**
    * @param args the command line arguments
    */
    
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                AddBoothProduct dialog = new AddBoothProduct(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel USD;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cbbBoothName;
    private javax.swing.JComboBox cbbCategory;
    private javax.swing.JComboBox cbbSizeUnit;
    private javax.swing.JComboBox cbbStatus;
    private javax.swing.JComboBox cbbWeight;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblBoothname;
    private javax.swing.JLabel lblCategory;
    private javax.swing.JLabel lblCost;
    private javax.swing.JLabel lblDescription;
    private javax.swing.JLabel lblMsBoothName;
    private javax.swing.JLabel lblMsCategory;
    private javax.swing.JLabel lblMsCost;
    private javax.swing.JLabel lblMsName;
    private javax.swing.JLabel lblMsSize;
    private javax.swing.JLabel lblMsWeight;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblSize;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JLabel lblWeight;
    private javax.swing.JTextArea txaDescription;
    private javax.swing.JFormattedTextField txfCost;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtSizeX;
    private javax.swing.JTextField txtSizeY;
    private javax.swing.JTextField txtSizeZ;
    private javax.swing.JTextField txtWeight;
    // End of variables declaration//GEN-END:variables
    private int idProduct = 0;
}
